// APP
if (window.plus) {
	bindaccountPageReady();
} else {
	document.addEventListener('plusready', bindaccountPageReady, false);
}
// Web
$(function() {
		if (AGENT_CLIENT.WEB) {
			bindaccountPageReady();
		}
	})
	// 初始化方法
function bindaccountPageReady() {
	var _thirdaccountInfo = getPageParams(PAGE_NAME.account_bindaccount);
  var _regionOption = (_thirdaccountInfo.mainid == -1);
	var _regionOption = true;
	var bindaccountPage = function() {
		this.init();
	};
	bindaccountPage.prototype = {
		// 初始化页面
		init: function() {
			if (_regionOption) {
				$("#confirmPwdDiv").show();
			}
			this.bindEvent();
		},
		// 绑定账号
		bindEvent: function() {
			var thispage = this;
			// 返回
			$("#back").on("click", function() {
				if (AGENT_CLIENT.APP) {
					back();
				} else {
					redirect("../home/index.html");
				}
			});
			//点击获取验证码
			$("#getVcode").on("click", function() {
				if ("" == $("#phone").val()) {
					$("#phone").focus();
					$.toast("请输入手机号");
					return;
				}
				if(!isTelephone($("#phone").val())){
					$.toast("请输入正确的手机号");
					return;				
				}
				
				$.ajax({
					headers: {'Content-Type': 'application/json'},
					type: 'POST',
					url: CONFIG_MANAGER.RequstUrl + "v1/account/check",
					data: $("#phone").val(),
					dataType: "json"
				}).done(function(res) {
					console.log(res);
					if (res && res.succeed) {
						$.toast($("#phone").val() + "已注册，请更换手机号！");
					} else {
						change_code($("#getVcode"), $("#phone"), SMS_TYPE.verify);
					}
				});

			});
			// 立即绑定
			$("#btnbind").on("click", function() {
				thispage.register();
			});
		},
		// 注册第三方账号
		register: function() {
			if ("" == $("#phone").val()) {
				$("#phone").focus();
				$.toast("请输入手机号");
				return;
			}
			if(!isTelephone($("#phone").val())){
				$.toast("请输入正确的手机号");
				return;				
			}
			if ("" == $("#vcode").val()) {
				$("#vcode").focus();
				$.toast("请输入验证码");
				return;
			}
			if ("" == $("#nPassword").val()) {
				$("#nPassword").focus();
				$.toast("请设置登录密码");
				return;
			}
			if (_regionOption && $("#nPassword").val() != $("#confirmNpassword").val()) {
				$("#confirmNpassword").focus();
				$.toast("两次输入密码不一样");
				return;
			}
			var model = this;
			var ip = $.getIPAddress();
			var postdata = {
				type: _thirdaccountInfo.typeid,
				id: _thirdaccountInfo.openid,
				name: _thirdaccountInfo.name ? _thirdaccountInfo.name : $("#phone").val().trim(),
				main_id: _thirdaccountInfo.mainid,
				password: $("#nPassword").val(),
				phone: $("#phone").val().trim(),
				vcode: $("#vcode").val().trim(),
				org: 6010,
				ip: ip ? ip : "127.0.0.1"
			};
			var requesturl = _regionOption ? "v1/account/third/register" : "v1/account/third/binding";
			console.log(JSON.stringify(postdata));
			$.ajax({
				headers: {
					'Content-Type': 'application/json'
				},
				type: 'POST',
				url: CONFIG_MANAGER.RequstUrl + requesturl,
				data: JSON.stringify(postdata),
				beforesend: function() {
					$.showWaiting();
				},
				dataType: "json"
			}).done(function(res) {
				$.toast("绑定成功");
				// 绑定
				if (_regionOption) {
					// 注册
					if (AGENT_CLIENT.APP) {
						var opener = plus.webview.currentWebview().opener();
						if (opener && opener.id.indexOf('login.html') > -1) {
							opener.close();
						}
						_verfiyAccountModel.verifyAccount({
							typeid: postdata.type,
							openid: postdata.id,
							name: postdata.name
						});
					} else {
						redirect("../home/index.html");
					}
				} else {
					// 绑定
					back();
				}
			}).fail(function(res) {
				var response = JSON.parse(res.responseText);
				// 未关联该账号，注册
				if (response && response.Message.indexOf("重复") > -1) {
					$.alert(postdata.phone + "已注册，请更换手机号！");
				} else {
					$.alert(response.Message);
				}
			}).always(function() {
				var opener = plus.webview.currentWebview().opener();
				if ("../account/login.html" == opener.id) {
					opener.close();
				}
				$.closeWaiting();
			});
		},
	};
	var _bindaccountPage = new bindaccountPage();
	// 验证用户信息
	var _verfiyAccountModel = new verfiyAccountModel();
}